package com.example.administrator.mypictureshow;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;

public class MainActivity extends AppCompatActivity {

    Button btn_selectPicture;
    private TextView text_path;
    Button btn_findtxt;
    //照片选择权限
    private static final int PERMISSION_REQUEST_STORAGE = 200;

    private static final int RESULT_ALBUNCODE = 199;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_selectPicture = (Button) this.findViewById(R.id.btn_selectPicture);
        text_path = (TextView) this.findViewById(R.id.text_path);
        btn_selectPicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT >= 23) {
                    int permissionResult = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
                    if (permissionResult == PackageManager.PERMISSION_GRANTED) {
                        jumpToAlbum();
                    } else if (permissionResult == PackageManager.PERMISSION_DENIED) {
                        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_STORAGE);
                    }
                } else {
                    jumpToAlbum();
                }

            }
        });

        //本地电子书格式查找
        btn_findtxt = (Button) this.findViewById(R.id.btn_findtxt);
        btn_findtxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listFiles();
            }
        });
    }
    private static void listFiles(){
        File sdRoot = new File(Environment.getExternalStorageDirectory() + "/");
        Collection<File> collection = FileUtils.listFiles(sdRoot, new String[]{"txt"}, true);
        for (File item : collection) {
            Log.e("Name: ",item.getName()+"");
            Log.e("Path: ", item.getAbsolutePath() + "");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            //相册返回处理
            case RESULT_ALBUNCODE:
                    if(RESULT_OK == resultCode){
                        ArrayList<String> pathList = data.getStringArrayListExtra("pathList");
                        if(null != pathList && pathList.size()>0){
                            Log.e("mainActivity","得到数据==="+pathList.size());
                            text_path.setText(pathList.toString());
                        }
                    }
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case PERMISSION_REQUEST_STORAGE:
                int permissionResult = grantResults[0];
                if (permissionResult == PackageManager.PERMISSION_GRANTED) {
                    jumpToAlbum();
                } else {
                    Toast.makeText(MainActivity.this,"请打开相关权限",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    private void jumpToAlbum(){
        Intent intent = new Intent(MainActivity.this,AlbumActivity.class);
        startActivityForResult(intent,RESULT_ALBUNCODE);
    }
}
